package com.ruoyi.mp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.mp.convert.MpMaterialConvert;
import com.ruoyi.mp.domain.MpMaterial;
import com.ruoyi.mp.domain.bo.MpMaterialBo;
import com.ruoyi.mp.domain.vo.material.MpMaterialUploadNewsImageReqVO;
import com.ruoyi.mp.domain.vo.material.MpMaterialUploadPermanentReqVO;
import com.ruoyi.mp.domain.vo.material.MpMaterialUploadRespVO;
import com.ruoyi.mp.domain.vo.material.MpMaterialUploadTemporaryReqVO;
import com.ruoyi.mp.service.IMpMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 素材管理Controller
 *
 * @author yh
 * @date 2024-01-26
 */
@RestController
@RequestMapping("/mp/material")
public class MpMaterialController extends BaseController
{
    @Autowired
    private IMpMaterialService mpMaterialService;

    /**
     * 上传临时素材
     * @param reqVO
     * @return
     * @throws IOException
     */
    @PostMapping("/upload-temporary")
    @SaCheckPermission("mp:material:edit")
    public R<MpMaterialUploadRespVO> uploadTemporaryMaterial(
            @Valid MpMaterialUploadTemporaryReqVO reqVO) throws IOException {
        MpMaterial material = mpMaterialService.uploadTemporaryMaterial(reqVO);
        return R.ok(MpMaterialConvert.convert(material));
    }

    /**
     * 上传永久素材
     * @param reqVO
     * @return
     * @throws IOException
     */
    @PostMapping("/upload-permanent")
    @SaCheckPermission("mp:material:edit")
    public R<MpMaterialUploadRespVO> uploadPermanentMaterial(
            @Valid MpMaterialUploadPermanentReqVO reqVO) throws IOException {
        MpMaterial material = mpMaterialService.uploadPermanentMaterial(reqVO);
        return R.ok(MpMaterialConvert.convert(material));
    }

    /**
     * 删除素材
     * @param id
     * @return
     */
    @DeleteMapping("/delete-permanent")
    @SaCheckPermission("mp:material:edit")
    public R<Boolean> deleteMaterial(@RequestParam("id") Long id) {
        mpMaterialService.deleteMpMaterialById(id);
        return R.ok(true);
    }

    /**
     * 上传图文内容中的图片
     * @param reqVO
     * @return
     * @throws IOException
     */
    @PostMapping("/upload-news-image")
    @SaCheckPermission("mp:material:edit")
    public R<String> uploadNewsImage(@Valid MpMaterialUploadNewsImageReqVO reqVO)
            throws IOException {
        return R.ok("ok",mpMaterialService.uploadNewsImage(reqVO));
    }

    /**
     * 获得素材分页
     * @param mpMaterial
     * @return
     */
    //TODO 重新实现，mybatisplus
    @GetMapping("/page")
    @SaCheckPermission("mp:material:edit")
    public TableDataInfo getMaterialPage(@Valid MpMaterialBo mpMaterial, PageQuery pageQuery) {
        return mpMaterialService.queryPageList(mpMaterial, pageQuery);
    }

}
